package hoyo.zqy.order.service.impl;

import hoyo.zqy.basic.service.impl.BaseServiceImpl;
import hoyo.zqy.basic.util.LoginContext;
import hoyo.zqy.basic.util.PageList;
import hoyo.zqy.order.domain.OrderPetAcquisition;
import hoyo.zqy.order.mapper.OrderPetAcquisitionMapper;
import hoyo.zqy.order.query.OrderPetAcquisitionQuery;
import hoyo.zqy.order.service.IOrderPetAcquisitionService;
import hoyo.zqy.org.domain.Employee;
import hoyo.zqy.org.domain.Shop;
import hoyo.zqy.org.mapper.EmployeeMapper;
import hoyo.zqy.org.mapper.ShopMapper;
import hoyo.zqy.pet.domain.Pet;
import hoyo.zqy.pet.domain.SearchMasterMsg;
import hoyo.zqy.pet.dto.OrderDto;
import hoyo.zqy.pet.mapper.PetMapper;
import hoyo.zqy.pet.mapper.SearchMasterMsgMapper;
import hoyo.zqy.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;

/**
 * 业务实现类：
 */
@Service
public class OrderPetAcquisitionServiceImpl extends BaseServiceImpl<OrderPetAcquisition> implements IOrderPetAcquisitionService {

    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    @Autowired
    private PetMapper petMapper;
    
    /**
     * 进阶分页
     * @param query
     * @param request
     * @return
     */
    @Override
    public PageList<OrderPetAcquisition> queryList(OrderPetAcquisitionQuery query, HttpServletRequest request) {
        //获取登录人信息
        Logininfo logininfo = LoginContext.getLogininfo(request);
        //获取管理员信息
        Employee employee = employeeMapper.loadByLogininfoId(logininfo.getId());
        //判断是平台还是店铺管理员
        if (employee.getShop_id()!=null){
            //是店铺的人
            query.setShopId(employee.getShop_id());
            //判断是店长还是店员
            Shop shop = shopMapper.loadById(employee.getShop_id());
            System.out.println(shop);
            if (shop.getAdminId()!=employee.getId()){
                //是店员
                query.setEmpId(employee.getId());
            }
        }

        return super.queryPage(query);
    }

    /**
     * 确认订单
     * @param orderDto
     */
    @Override
    public void confirm(OrderDto orderDto) {
        //1.改订单状态 0待处理 - 1已处理
        //2.修改订单的支付方式和金
        OrderPetAcquisition order = orderPetAcquisitionMapper.loadById(orderDto.getId());
        SearchMasterMsg msg = searchMasterMsgMapper.loadById(order.getSearchMasterMsgId());
        Pet pet = msg2Pet(msg,orderDto.getMoney());
        petMapper.save(pet);
        //2.改订单状态为1，价格为成交价，支付方式
        order.setState(1);
        order.setPaytype(orderDto.getPayType());
        order.setPrice(orderDto.getMoney());
        order.setPetId(pet.getId());
        orderPetAcquisitionMapper.update(order);
        //3.改寻主消息状态2
        msg.setState(2);
        searchMasterMsgMapper.update(msg);

    }

    /**
     * 订单完成修改宠物信息
     * @param msg
     * @param money
     * @return
     */
    private Pet msg2Pet(SearchMasterMsg msg, BigDecimal money) {
        Pet pet = new Pet();
        pet.setName(msg.getName());
        pet.setCostprice(money);
        pet.setTypeId(msg.getPetType());
        pet.setResources(msg.getResources());
        pet.setShopId(msg.getShopId());
        pet.setUserId(msg.getUserId());
        return pet;
    }


    /**
     * 取消订单
     * @param id
     */
    @Override
    public void delete(Long id) {
        //将订单放入寻主池
        OrderPetAcquisition order = orderPetAcquisitionMapper.loadById(id);
        searchMasterMsgMapper.reject(order.getSearchMasterMsgId());
        //修改订单状态
        order.setState(-1);
        orderPetAcquisitionMapper.update(order);
    }
}
